package com.pda.yuanjing.pages.custombill.add;

import android.content.Context;
import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.beanin.AddCBillBeanIn;
import com.pda.yuanjing.beanin.DownBaseBeanIn;
import com.pda.yuanjing.beanout.BaseBeanOut;
import com.pda.yuanjing.beanout.ProductBeanOut;
import com.pda.yuanjing.beanout.StoreBeanOut;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class AddBillModelImpl extends BaseModel implements AddBillModel {
    private String TAG = "AddBillModelImpl";
    private BaseBeanOut baseBeanOut;

    public AddBillModelImpl(Context context) {
        super(context);
    }

    @Override
    public void addBill(AddCBillBeanIn addCBillBeanIn, CallBack callBack) {
        try {
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(addCBillBeanIn));
            api.downloadBill(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.ontip("");
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (baseBeanOut != null && baseBeanOut.getErrmsg() !=null) {
                                callBack.ontip(baseBeanOut.getErrmsg());
                            } else {
                                callBack.ontip(SetConfig.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            baseBeanOut = gson.fromJson(StringProcess.responsetoString(abBean), BaseBeanOut.class);
                            if (baseBeanOut != null && baseBeanOut.getErrcode() == 0) {
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(SetConfig.connectError);
            e.printStackTrace();
        }
    }
    @Override
    public  int downLoadInfo1(String processID,String jieDianName) {
        try {
            PDApp.productBeanOutt.setErrcode(-1);
            PDApp.productBeanOutt.setData(null);
            DownBaseBeanIn downBaseBeanIn = new DownBaseBeanIn();
            downBaseBeanIn.setAction("DownloadBaseInfo");
            downBaseBeanIn.setCid(SharedPreferencesManger.getCid());
            downBaseBeanIn.setUserID(SharedPreferencesManger.getUid());
            downBaseBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            downBaseBeanIn.setProcessID(processID);
            downBaseBeanIn.setJieDianName(jieDianName);
            downBaseBeanIn.setBaseType("1");
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(downBaseBeanIn));
            final Call<ResponseBody> call = apisync.downLoad(body);
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Response<ResponseBody> response = call.execute();
                        if (response.isSuccessful()) {
                            LogUtils.e(StringProcess.responsetoString(response.body()));
                            PDApp.productBeanOutt = gson.fromJson(StringProcess.responsetoString(response.body()), ProductBeanOut.class);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return PDApp.productBeanOutt.getErrcode();
        }
    }

    @Override
    public int downLoadInfo2(String processID,String jieDianName) {
        try {
            PDApp.storeBeanOutt.setErrcode(-1);
            PDApp.storeBeanOutt.setData(null);
            DownBaseBeanIn downBaseBeanIn = new DownBaseBeanIn();
            downBaseBeanIn.setAction("DownloadBaseInfo");
            downBaseBeanIn.setCid(SharedPreferencesManger.getCid());
            downBaseBeanIn.setUserID(SharedPreferencesManger.getUid());
            downBaseBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            downBaseBeanIn.setProcessID(processID);
            downBaseBeanIn.setJieDianName(jieDianName);
            downBaseBeanIn.setBaseType("2");
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(downBaseBeanIn));
            final Call<ResponseBody> call = apisync.downLoad(body);
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Response<ResponseBody> response = call.execute();
                        if (response.isSuccessful()) {
                            LogUtils.e(StringProcess.responsetoString(response.body()));
                            PDApp.storeBeanOutt = gson.fromJson(StringProcess.responsetoString(response.body()), StoreBeanOut.class);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return PDApp.storeBeanOutt.getErrcode();
        }
    }

}

